package org.example.browser;

import com.sun.deploy.security.SelectableSecurityManager;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import javax.xml.bind.Element;

import java.util.List;
import java.util.concurrent.TimeUnit;

import static java.lang.Thread.sleep;

/**
 * Created with Intellij IDEA.
 * Description;
 * User:TQ02
 * Data:2024-05-31
 * Time:10:19
 * Text：
 */
public class Main {
    public static void test1() throws InterruptedException {
        ChromeOptions options = new ChromeOptions();
        options.setBinary("D:\\Chrome\\chrome.exe");
        options.addArguments("---remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);
        webDriver.get("http://www.baidu.com");
        //找到百度搜索框
//        WebElement element = webDriver.findElement(By.cssSelector(""));
        WebElement element = webDriver.findElement(By.xpath("//*[@id=\"kw\"]"));
        //输入框输入
        element.sendKeys("我喜欢你");
        //找到百度按钮
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(30000);
        webDriver.manage().timeouts().implicitlyWait(3, TimeUnit.DAYS);
        //找到搜索结果
        List<WebElement> elements= webDriver.findElements(By.cssSelector("a em"));
        for(int i=0;i<elements.size();i++){
            System.out.println(elements.get(i).getText());
            if(elements.get(i).getText().equals("软件测试")){
                System.out.println("测试通过");
                break;
            }
        }
        System.out.println("Hello world!");
    }
    public static void test2(){
        ChromeOptions options = new ChromeOptions();
        options.setBinary("D:\\Chrome\\chrome.exe");
        options.addArguments("---remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);
        webDriver.get("http://www.baidu.com");
        //报错，原因是不在form标签里
        webDriver.findElement(By.xpath("//a[text()=\"新闻\"]")).submit();
    }
    private static void test3() {
        ChromeOptions options = new ChromeOptions();
        options.setBinary("D:\\Chrome\\chrome.exe");
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);
        webDriver.get("https://www.baidu.com/");
        String button_value = webDriver.findElement(By.cssSelector("#su")).getAttribute("value");
//        System.out.println(button_value);
        if(button_value.equals("百度一下")) {
            System.out.println("测试通过");
        } else {
            System.out.println(button_value);
            System.out.println("测试不通过");
        }
    }
    private static void test4() {
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--remote-allow-origins=*");
        WebDriver webDriver = new ChromeDriver(options);
        webDriver.get("https://www.baidu.com/");
        String url = webDriver.getCurrentUrl();
        String title = webDriver.getTitle();
        if(url.equals("https://www.baidu.com/") && title.equals("百度一下，你就知道")) {
            System.out.println("当前页面url：" + url + "，当前页面title：" + title);
            System.out.println("测试通过");
        } else {
            System.out.println("测试不通过");
        }
    }
    public static void main(String[] args) throws InterruptedException {
            test3();
    }
}